package com.xopencode.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author Mcb
 * @date 2022/5/2
 * @apiNote 通过nio创建文件
 */
public class ChannelReadTest {
    public static void main(String[] args) throws IOException {
        String fileName = "NioTest.txt";
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        FileChannel channel = fis.getChannel();
        //创建缓冲区
        //ByteBuffer buffer = ByteBuffer.allocate(1024); //这种方式会打印后续没有填充得位置
        ByteBuffer buffer = ByteBuffer.allocate((int) file.length());
        channel.read(buffer);
        System.out.println(new String(buffer.array()));
        fis.close();
    }
}
